﻿
@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 45% !important;
    }
</style>

<div ng-controller="RoomDetailController">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
                    <!-- START panel-->
                    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
                        <div class="panel-body">
                            <fieldset class="b0">
                                <legend>{{Room.Id == 0 || Room.Id == null ? '添加公寓' : '修改公寓信息'}}</legend>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">公寓号</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="RoomNO" id="RoomNO" required="" ng-model="Room.RoomNO" class="form-control" maxlength="10"  />
                                        <span ng-show="validateInput('RoomNO', 'required')" class="text-danger">请输入公寓号</span>
                                        <span ng-show="IsRightRoomNO" class="text-danger">公寓号只能为数字</span>
                                        <span ng-show="IsCheckRoomNO" class="text-danger">输入公寓号已经存在</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">楼栋</label>
                                    <div class="col-sm-4">
                                        <select ng-options="Building.Id as Building.BuildingNO for Building in BuildingList" ng-model="Room.BuildingId" class="form-control" name="BuildingNO" required="" id="sel_BuildingNO">
                                            <option value="">请选择楼栋</option>
                                        </select>
                                        <span ng-show="validateInput('BuildingNO', 'required')" class="text-danger">请选择楼栋</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">公寓性质</label>
                                    <div class="col-sm-4">
                                        <select ng-options="RoomAttr.Code as RoomAttr.CodeName for RoomAttr in RoomAttrList" ng-model="Room.RoomAttr" class="form-control" name="RoomAttr" required="" id="sel_RoomAttr">
                                            <option value="">请选择公寓性质</option>
                                        </select>
                                        <span ng-show="validateInput('RoomAttr', 'required')" class="text-danger">请选择公寓性质</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">公寓类型</label>
                                    <div class="col-sm-4">
                                        <select ng-options="RoomType.Code as RoomType.CodeName for RoomType in RoomTypeList" ng-model="Room.RoomType" class="form-control" name="RoomType" required="" id="sel_RoomType">
                                            <option value="">请选择公寓类型</option>
                                        </select>
                                        <span ng-show="validateInput('RoomType', 'required')" class="text-danger">请选择公寓类型</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            
                        </div>
                        <div class="panel-footer text-right">
                            <button type="button" class="btn btn-warning" ng-click="cancel()">返回列表</button>
                            <button type="button" class="btn btn-primary" ng-click="save()">保存</button>
                        </div>
                        
                    </div>
                    <!-- END panel-->
                </form>
            </div>
        </div>
    </div> 
</div>


<div id='container' ng-show="false"></div>


<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=56aaa1c52d140b9b3276d2893997b0ba"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>

<script type="text/javascript">
    App.controller('RoomDetailController', ["$scope", "$state", "$http", "$stateParams", "Notify", "$filter",
        function ($scope, $state, $http, $stateParams, Notify, $filter) {
            'use strict';
            $scope.isLoading = true;
            
            $scope.IsRightRoomNO = false;
            $scope.IsCheckRoomNO = false;

            $scope.DownloadFile = "~/Templete/User/人员导入模板.xlsx";

            $scope.validateInput = function (name, type) {
                var input = $scope.formValidate[name];
                return (input.$dirty || $scope.submitted) && input.$error[type];
            };
            //数据初始化
            $http.post("/Room/GetRoomInitDetail", { id: $stateParams.id }).then(function (response) {
                
                $scope.Room = angular.copy(response.data.RoomInfo);

                $scope.RoomTypeList = angular.copy(response.data.RoomTypeList);
                $scope.RoomAttrList = angular.copy(response.data.RoomAttrList);
                $scope.BuildingList = angular.copy(response.data.BuildingInfoList);

                if ($scope.Room == null) {
                    $scope.Room = {};
                }
                $scope.isLoading = false;
            });
            
            // 保存点击
            $scope.save = function () {
                
                $scope.submitted = true;
                //判断公寓号
                var testRoomNO = /^[0-9]*$/;

                if (testRoomNO.test($scope.Room.RoomNO) === false) {
                    $scope.IsRightRoomNO = true;
                    var txt = document.getElementById('RoomNO')
                    txt.focus();
                    txt.select();
                    return false;
                } else {
                    $scope.IsRightRoomNO = false;
                }
                //
                if ($stateParams.id != 0)
                {
                    $scope.isLoading = true;
                    if ($scope.formValidate.$valid) {
                        $http.post("/Room/RoomSave", $scope.Room).then(function (returnData) {
                            if (returnData.data.isSuccess) {
                                Notify.alert('操作成功!', { status: 'success' });
                                $state.go("app.roomlist");
                            } else {
                                Notify.alert(returnData.data.msg, { status: 'warning' });
                                $scope.isLoading = false;
                            }
                        });

                    } else {
                        $scope.isLoading = false;
                    }
                    return;
                }
                //检查公寓是否存在
                $http.post("/Room/RoomCheck", { RoomNO: $scope.Room.RoomNO, BuildingId: $scope.Room.BuildingId }).then(function (returnData) {
                    
                    if (returnData.data.isSuccess) {
                        $scope.IsCheckRoomNO = false;
                        //保存公寓信息

                        $scope.isLoading = true;
                        if ($scope.formValidate.$valid) {
                            $http.post("/Room/RoomSave", $scope.Room).then(function (returnData) {
                                if (returnData.data.isSuccess) {
                                    Notify.alert('操作成功!', { status: 'success' });
                                    $state.go("app.roomlist");
                                } else {
                                    Notify.alert(returnData.data.msg, { status: 'warning' });
                                    $scope.isLoading = false;
                                }
                            });

                        } else {
                            $scope.isLoading = false;
                        }
                        
                    } else {
                        $scope.IsCheckRoomNO = true;
                        var txt = document.getElementById('RoomNO')
                        txt.focus();
                        txt.select();
                        
                    }
                });
                
            };

            //返回列表
            $scope.cancel = function () {
                $state.go("app.roomlist");
            };

        }]);
</script>
